/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 * painting layer
 *
 * User: jinchen
 * Date: Jun 10, 2003
 * Time: 1:33:22 PM
 */
package edu.psu.geovista.app.pcp.plot.layer;

import edu.psu.geovista.app.pcp.plot.gui.BasicPcpUI;
import edu.psu.geovista.app.pcp.plot.layer.event.LayerEvent;
import edu.psu.geovista.app.pcp.plot.layer.event.LayerListener;

import edu.psu.geovista.app.plot.axis.AxisList;
import edu.psu.geovista.app.plot.axis.XAxisOrder;
import edu.psu.geovista.app.plot.layer.Layer;

import javax.swing.event.EventListenerList;
import java.awt.*;
import java.awt.image.BufferedImage;

public abstract class BasicLayer implements
                    Layer,Comparable{
    public static final boolean DEBUG=false;
    public static final Color TRANSPARENT=new Color(0,0,0,0);

    /*protected  boolean scaled=false;
    protected  RenderedImage scaledImage;*/


    //protected  BufferedImage image;//layer image
    protected  int level;//layer level

    protected  int type; // axis, data, dynamic...
    protected  Dimension size=new Dimension(); //layer image size
    protected  boolean repaint=true;
    protected  boolean visible=true;
    protected  Color color;//default color to paint a layer
    protected  String name;


    private boolean persisted=true;// if true, has persisted image.Painting will draw the image on the canvas. Otherwise, draw layer at run-time

    protected  AxisList yaxisList; //? list currently visible axes
    protected  XAxisOrder xaxis;

    protected Composite composite;
    protected ConfigInfo configInfo=new DefaultConfigInfo();

    protected EventListenerList listenerList = new EventListenerList();


    BasicPcpUI ui;

    /*public BasicLayer(BasicPcpUI owner) {
        this.owner = owner;
    }*/

    public static BufferedImage getBlankImage(Dimension size){
        BufferedImage image=null;
        try {


            image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_4BYTE_ABGR);//BufferedImage.TYPE_INT_ARGB
          


        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use Options | File Templates.
        }
        catch (Error e) {//out of memory error
             System.out.println("BasicLayer.getBlankImage() throw "+e);
             e.printStackTrace();  //To change body of catch statement use Options | File Templates.
        }

        return   image ;
    }
    /**
     *  paint this layer
     */
    public void paintLayer(){

    }
    public void setPaintMode(int mode){}

    //Decide how the layer is painted
    protected  abstract void paintComponent(Graphics g);

    public Graphics2D getImagePainter() /*throws Exception*/ {return null;}


     /**
     *  make existing image of the layer as a blank transparent image
     */
    public  void clearLayer(){

    }

    /*******************************************************************************************************
     *                event
     *******************************************************************************************************/
    public void addLayerListener(LayerListener l) {
        this.listenerList.add(LayerListener.class,l);
    }
    public void removeLayerListener(LayerListener l) {
        this.listenerList.remove(LayerListener.class,l);
    }

    public void fireLayerChangeEvent() {
       Object[] listeners=this.listenerList.getListenerList() ;
       LayerEvent e=null;
       for (int i=listeners.length -2;i>=0;i-=2){
           if (listeners[i]==LayerListener.class){
               e=new LayerEvent(this);
           }
           ((LayerListener)listeners[i+1]).layerChange(e);

       }
    }
    /*******************************************************************************************************
     *                properties
     *******************************************************************************************************/
    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }




    public boolean isRepaint() {
        return repaint;
    }

    public void setRepaint(boolean repaint) {
        this.repaint = repaint;
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int compareTo(Object o) {
            Layer otherLayer=(Layer)o;
            int tl=this.getLevel() ;
            int ol=otherLayer.getLevel() ;
            if (tl<ol){
                return -1;
            }
            else if (tl>ol){
                return 1;
            }
            else{
                return 0;
            }

     }

    public void setComposite(Composite composite) {
        this.composite = composite;
    }

    public Composite getComposite() {
        return composite;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public ConfigInfo getConfigInfo() {
        return configInfo;
    }

    public void setAConfigInfo(String name,Object value) {
        this.configInfo.setConfig(name,value);
    }

    public BasicPcpUI getUi() {
        return ui;
    }

    public void setUi(BasicPcpUI ui) {
        this.ui = ui;
    }

    public boolean isPersisted() {
        return persisted;
    }

    public void setPersisted(boolean persisted) {
        this.persisted = persisted;
    }
     public Dimension getSize() {
           /* if (this.size ==null)
                this.size =new Dimension();   */
            return size;
        }

        public void setSize(Dimension size) {
            this.size.setSize(size); //refer to single size object in PcpPlot
            //this.getSize().setSize(size);
        }


}
